//
//  RxHandyJSON.swift
//  TestAPIManager
//
//  Created by 杨庆 on 2020/9/28.
//  Copyright © 2020 杨庆. All rights reserved.
//

import Foundation
import RxSwift
import HandyJSON
import Moya

enum DCUError:Swift.Error {
    case ParseJSONError
    case RequestFailed
    case NoResponse
    case UnexpectedResult(resultCode:Int?,resultMsg:String?)
}

enum RequestStatus: Int {
    case requestSuccess = 200
    case requestError
}

fileprivate let RESULT_CODE = "code"
fileprivate let RESULT_MSG = "reason"
fileprivate let RESULT_DATA = "result"

/*RxSwift*/
public extension Observable {
    
//    func mapResponseToObject<T: HandyJSON>(type: T.Type) -> Observable<T> {
//
//        /* 将可观察序列的每个元素投射到新的形式中 */
//        return map { response in
//
//            /*Moya*/
//            guard let response = response as? Moya.Response
//                else {
//                    throw DCUError.NoResponse
//            }
//            guard ((200...209) ~= response.statusCode) else {
//                throw DCUError.RequestFailed
//            }
//            let jsonData = try response.mapJSON() as! [String : Any]
//            if let code = jsonData[RESULT_MSG] as? String {
//                if code == "查询成功" {
//                    if let model = JSONDeserializer<T>.deserializeFrom(dict: jsonData){
//                        return model
//                    }
//                }
//            }
//        }
//    }
    
    
}


//public extension Observable {
//    func mapResponseToObject<T:HandyJSON>(type:T.Type) -> Observable<T> {
//        return map { response  in
//
//            guard let response = response as? Moya.Response else {
//                throw DCUError.NoResponse
//            }
//            guard ((200 ... 209) ~= response.statusCode) else {
//                throw DCUError.RequestFailed
//            }
//
//            let jsonData = try response.mapJSON() as! [String:Any]
//            if let code = jsonData[RESULT_MSG] as? String {
//                if code == "查询成功" {
//                    if let model = JSONDeserializer<T>.deserializeFrom(dict: jsonData){
//                        return model
//                    }
//                }
//            }
//        }
//    }
//}

